/**
 * 路由配置文件（优化版）
 * 
 * @file frontend/src/routes/index-optimized.tsx
 * @description 优化的路由配置，保持类型安全的同时提升可读性
 * @author 类小脑计算平台开发团队
 * @date 2025-11-15
 * 
 * 优化点：
 * 1. 使用辅助函数简化重复代码
 * 2. 路由配置更加紧凑清晰
 * 3. 保持完整的类型安全和 IDE 支持
 * 4. 使用分组和注释提升可读性
 */

import React, { lazy, Suspense, ComponentType } from 'react';
import { RouteObject, Navigate } from 'react-router-dom';

// ============================================================================
// 辅助组件和函数
// ============================================================================

/**
 * 加载中组件
 */
const LoadingFallback: React.FC = () => (
  <div style={{
    display: 'flex',
    justifyContent: 'center',
    alignItems: 'center',
    height: '100vh',
    fontSize: '18px',
    color: '#666'
  }}>
    加载中...
  </div>
);

/**
 * 创建懒加载路由的辅助函数
 * @param Component 懒加载的组件
 * @returns 包装了 Suspense 的 React 元素
 */
const withSuspense = (Component: React.LazyExoticComponent<ComponentType<any>>) => (
  <Suspense fallback={<LoadingFallback />}>
    <Component />
  </Suspense>
);

/**
 * 创建路由对象的辅助函数
 * @param path 路由路径
 * @param Component 懒加载的组件
 * @returns 路由对象
 */
const createRoute = (
  path: string,
  Component: React.LazyExoticComponent<ComponentType<any>>
): RouteObject => ({
  path,
  element: withSuspense(Component),
});

/**
 * 创建重定向路由的辅助函数
 * @param from 源路径
 * @param to 目标路径
 * @returns 路由对象
 */
const createRedirect = (from: string, to: string): RouteObject => ({
  path: from,
  element: <Navigate to={to} replace />,
});

// ============================================================================
// 页面组件懒加载
// ============================================================================

// 主要页面
const Welcome = lazy(() => import('../pages/Welcome'));
const Main = lazy(() => import('../pages/Main'));
const NetPyNE = lazy(() => import('../components/NetPyNE/NetPyNE'));

// 模型定义
const Populations = lazy(() => import('../pages/Populations'));
const SynMechs = lazy(() => import('../pages/SynMechs'));
const ConnRules = lazy(() => import('../pages/ConnRules'));
const SubcellConnRules = lazy(() => import('../pages/SubcellConnRules'));
const StimSources = lazy(() => import('../pages/StimSources'));
const StimTargets = lazy(() => import('../pages/StimTargets'));

// 仿真配置
const SimulationConfigGeneral = lazy(() => import('../pages/SimulationConfigGeneral'));
const SimulationConfigRecord = lazy(() => import('../pages/SimulationConfigRecord'));
const SimulationConfigNetwork = lazy(() => import('../pages/SimulationConfigNetwork'));
const SimulationConfigNetworkV2 = lazy(() => import('../pages/SimulationConfigNetworkV2'));
const SimulationConfigNetworkEditor = lazy(() => import('../pages/SimulationConfigNetworkEditor'));
const SimulationConfigSave = lazy(() => import('../pages/SimulationConfigSave'));
const SimulationConfigSaveV2 = lazy(() => import('../pages/SimulationConfigSaveV2'));

// 绘图设置
const PlotSettingsRaster = lazy(() => import('../pages/PlotSettingsRaster'));
const PlotSettingsTrajectory = lazy(() => import('../pages/PlotSettingsTrajectory'));
const PlotSettingsLFP = lazy(() => import('../pages/PlotSettingsLFP'));

// 反应扩散
const ReactionDiffusionRegion = lazy(() => import('../pages/ReactionDiffusionRegion'));
const ReactionDiffusionEmpty = lazy(() => import('../pages/ReactionDiffusionEmpty'));

// 导入功能
const ImportNeuroML = lazy(() => import('../pages/ImportNeuroML'));
const ImportPythonDialog = lazy(() => import('../pages/ImportPythonDialog'));

// 实验管理
const ExperimentManager = lazy(() => import('../pages/ExperimentManager'));

// 对话框
const SelectFolderDialog = lazy(() => import('../pages/SelectFolderDialog'));
const OpenJSONDialog = lazy(() => import('../pages/OpenJSONDialog'));
const SaveJSONDialog = lazy(() => import('../pages/SaveJSONDialog'));
const SaveJSONDialogOptions = lazy(() => import('../pages/SaveJSONDialogOptions'));
const DeleteConfirmDialog = lazy(() => import('../pages/DeleteConfirmDialog'));

// 特殊页面
const GeppettoTest = lazy(() => import('../pages/GeppettoTest'));

// ============================================================================
// 路由配置
// ============================================================================

/**
 * 主要页面路由
 */
const mainRoutes: RouteObject[] = [
  // 首页和主配置
  createRoute('/', Welcome),
  createRoute('/cell-config', Main),
  createRoute('/netpyne', NetPyNE),
  
  // 模型定义
  createRoute('/populations', Populations),
  createRoute('/syn-mechs', SynMechs),
  createRoute('/conn-rules', ConnRules),
  createRoute('/subcell-conn-rules', SubcellConnRules),
  createRoute('/stim-sources', StimSources),
  createRoute('/stim-targets', StimTargets),
  
  // 仿真配置
  createRoute('/simulation-config-general', SimulationConfigGeneral),
  createRoute('/simulation-config-record', SimulationConfigRecord),
  createRoute('/simulation-config-network', SimulationConfigNetwork),
  createRoute('/simulation-config-network-v2', SimulationConfigNetworkV2),
  createRoute('/simulation-config-network-editor', SimulationConfigNetworkEditor),
  createRoute('/simulation-config-save', SimulationConfigSave),
  createRoute('/simulation-config-save-v2', SimulationConfigSaveV2),
  
  // 绘图设置
  createRoute('/plot-settings-raster', PlotSettingsRaster),
  createRoute('/plot-settings-trajectory', PlotSettingsTrajectory),
  createRoute('/plot-settings-lfp', PlotSettingsLFP),
  
  // 反应扩散
  createRoute('/reaction-diffusion-region', ReactionDiffusionRegion),
  createRoute('/reaction-diffusion-empty', ReactionDiffusionEmpty),
  
  // 导入功能
  createRoute('/import-neuroml', ImportNeuroML),
  createRoute('/import-python', ImportPythonDialog),
  
  // 实验管理
  createRoute('/experiment-manager', ExperimentManager),
  
  // 对话框
  createRoute('/select-folder', SelectFolderDialog),
  createRoute('/open-json', OpenJSONDialog),
  createRoute('/save-json', SaveJSONDialog),
  createRoute('/save-json-options', SaveJSONDialogOptions),
  createRoute('/delete-confirm', DeleteConfirmDialog),
  
  // 特殊页面
  createRoute('/geppetto-test', GeppettoTest),
];

/**
 * 兼容性路由（重定向）
 */
const legacyRoutes: RouteObject[] = [
  createRedirect('/synaptic-mechanisms', '/syn-mechs'),
  createRedirect('/connectivity-rules', '/conn-rules'),
  createRedirect('/subcellular-connectivity', '/subcell-conn-rules'),
  createRedirect('/stimulation-sources', '/stim-sources'),
  createRedirect('/stimulation-targets', '/stim-targets'),
  createRedirect('/plot-settings-traces', '/plot-settings-trajectory'),
  createRedirect('/reaction-diffusion-extracellular', '/reaction-diffusion-empty'),
];

/**
 * Frame 路由（设计图编号重定向）
 */
const frameRoutes: RouteObject[] = [
  createRedirect('/frame/8', '/cell-config'),
  createRedirect('/frame/10', '/simulation-config-general'),
  createRedirect('/frame/13', '/plot-settings-raster'),
  createRedirect('/frame/14', '/plot-settings-trajectory'),
  createRedirect('/frame/15', '/reaction-diffusion-region'),
  createRedirect('/frame/16', '/plot-settings-lfp'),
  createRedirect('/frame/17', '/reaction-diffusion-empty'),
  createRedirect('/frame/18', '/simulation-config-record'),
  createRedirect('/frame/19', '/simulation-config-network'),
  createRedirect('/frame/20', '/simulation-config-save'),
  createRedirect('/frame/21', '/simulation-config-network-v2'),
  createRedirect('/frame/22', '/simulation-config-save-v2'),
  createRedirect('/frame/23', '/import-neuroml'),
  createRedirect('/frame/24', '/simulation-config-network-editor'),
  createRedirect('/frame/25', '/experiment-manager'),
  createRedirect('/frame/26', '/select-folder'),
  createRedirect('/frame/27', '/open-json'),
  createRedirect('/frame/28', '/save-json'),
  createRedirect('/frame/29', '/delete-confirm'),
  createRedirect('/frame/30', '/save-json-options'),
  createRedirect('/frame/31', '/import-python'),
];

// ============================================================================
// 导出路由配置
// ============================================================================

/**
 * 应用的完整路由配置
 * 
 * 路由统计：
 * - 主要页面路由：30 个
 * - 兼容性路由：7 个
 * - Frame 路由：21 个
 * - 总计：58 个路由
 */
const routes: RouteObject[] = [
  ...mainRoutes,
  ...legacyRoutes,
  ...frameRoutes,
];

export default routes;

// 导出分类路由（可选）
export {
  mainRoutes,
  legacyRoutes,
  frameRoutes,
};

